import wepy from 'wepy'

export default (params, imagePath) => {
  return new Promise((resolve, reject) => {
    const host = 'https://hz-vehicle.oss-cn-shenzhen.aliyuncs.com'
    const signature = params.signature
    const ossAccessKeyId = params.OSSAccessKeyId
    const policy = params.policy

    const fileType = imagePath.split('.').pop()
    const key = `${params.fileName}.${fileType}`
    const securityToken = '<x-oss-security-token>'
    const filePath = imagePath; // 待上传文件的文件路径。
    wx.uploadFile({
      url: host, // 开发者服务器的URL。
      filePath: filePath,
      name: 'file', // 必须填file。
      formData: {
        key,
        policy,
        OSSAccessKeyId: ossAccessKeyId,
        signature
        // 'x-oss-security-token': securityToken // 使用STS签名时必传。
      },
      success: (res) => {
        if (res.statusCode === 204) {
          console.log('上传成功', res)
          resolve({
            objectName: key,
            uploadRes: res
          })
        }
        resolve(false)
      },
      fail: err => {
        console.log('上传失败', err)
        resolve(true)
      }
    })
  })
}
